import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mvvm_provider/base/consumer_status_widget.dart';
import 'package:mvvm_provider/page/mine/data/model/info_model.dart';
import 'package:mvvm_provider/page/mine/states/mine_state.dart';
import 'package:mvvm_provider/routers/mine_router.dart';
import '../../../base/base_list_view.dart';
import '../../../base/base_stateful_page.dart';
import '../../../routers/navigator_utils.dart';
import '../view_model/mine_view_model.dart';
import '../widgets/info_item.dart';

class MinePage extends BasePage {
  const MinePage({super.key});

  @override
  BasePageState<BasePage> getState() => _MinePageState();
}

class _MinePageState extends BasePageState<MinePage> {
  @override
  void initState() {
    super.initState();
    super.pageTitle = '我的';
    pageBgColor = const Color(0xFFF3F4F8);
    isBack = false;
  }

  @override
  void buildComplete() {
    super.buildComplete();
    _onRefresh();
  }

  @override
  Widget left() {
    return const SizedBox();
  }

  /// 上拉加载
  void _onLoading() {
    getListData();
  }

  /// 下拉刷新
  void _onRefresh() {
    getListData(isRefresh: true);
  }

  void getListData({bool isRefresh = false}) {
    getViewModel<MineViewModel>().getListData(isRefresh);
  }

  @override
  Widget buildPage(BuildContext context) {
    return ConsumerStatusWidget<MineViewModel, MineState>(
      placeHolderType: PlaceHolderType.listViewPlaceHolder,
      builder: (context, state, child) {
        return BaseListView(
          refreshController: getViewModel<MineViewModel>().refreshController,
          scrollController: getViewModel<MineViewModel>().scrollController,
          bgColor: const Color(0xFFF3F4F8),
          enablePullDown: true,
          enablePullUp: true,
          data: state.dataList,
          onRefresh: _onRefresh,
          onLoading: _onLoading,
          itemBuilder: (InfoModel model, int index) => InfoWidget(
            model: model,
            onTap: () {
              NavigatorUtils.push(context, MineRouter.mineDetailPage, arguments: {
                'entityId': model.id,
              });
            },
          ),
        );
      },
    );
  }
}
